package com.wpf.servlet;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
//@WebServlet("/syncServlet1")
public class SyncServlet extends HttpServlet{

    public SyncServlet() {
//        log("同步Servlet的构造方法！");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        long start = System.currentTimeMillis();
        this.doWork(req,resp);
        long end = System.currentTimeMillis();
        System.out.println("同步总共消耗"+ (end-start) + "秒");
    }

    public static void doWork(HttpServletRequest req, HttpServletResponse resp) {
        // 模拟耗时操作
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {

        }

        //
        try {
            resp.getWriter().append("syn done");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
